package service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import common.ServerResponse;
import domain.address;
import domain.order_detail;
import domain.orders;
import service.IManageOrderService;
import util.jdbcUtil;
import vo.AddressVO;
import vo.OrderDetailVO;
import vo.OrderVO;

public class ManageOrderServiceImpl implements IManageOrderService {

	public ServerResponse list(String pageNum, String pageSize) {
		// TODO Auto-generated method stub
		String sql="select * from orders";
		List<orders> orderslist=jdbcUtil.select(orders.class, sql);
		int count=0;
		if(orderslist.size()>0) {
			count=orderslist.size();
		}
		else {
			return ServerResponse.serverResponseByError("error:暂时还没有订单");
		}
		String sqlsql="select * from orders limit ?,?";
		long pagenum=Long.parseLong(pageNum);
		long pagesize=Long.parseLong(pageSize);
		orderslist.clear();
	    orderslist=jdbcUtil.select(orders.class, sqlsql,(pagenum - 1)*pagesize, pagesize);
	    if(orderslist.size()<=0) {
	    	return ServerResponse.serverResponseByError("error:页码非法！！！，无数据");
	    }
	    //重要
	    return BackOrdervo(orderslist,count);
	}

	public ServerResponse search(String order_no) {
		// TODO Auto-generated method stub
		String sql="select * from orders where order_no=?";
		List<orders> list=jdbcUtil.select(orders.class, sql, order_no);
		if(list.size()<=0) {
			return ServerResponse.serverResponseByError("error:订单不存在");
		}
		return BackOrdervo(list, 0);
	}

	public ServerResponse detail(String order_no) {
		// TODO Auto-generated method stub
				String sql="select * from orders where order_no=?";
				List<orders> list=jdbcUtil.select(orders.class, sql, order_no);
				if(list.size()<=0) {
					return ServerResponse.serverResponseByError("error:订单不存在");
				}
				return BackOrdervo(list, 0);
	}

	public ServerResponse send_goods(String order_no) {
		// TODO Auto-generated method stub
		String sql1="select * from orders where order_no=?";
		String sql2="update orders set status=3,update_time=?,send_time=? where order_no=?";
		List<orders> list=jdbcUtil.select(orders.class, sql1, order_no);
		if(list.size()<=0) {
			return ServerResponse.serverResponseByError("error:订单不存在");
		}
		orders o=list.get(0);
		long status=o.getStatus();
		if(status!=2) {
			return ServerResponse.serverResponseByError("error:订单未处于已付款状态，可能处于其他状态！");
		}
		Date date = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time=sdf.format(date);
		int i=jdbcUtil.update(sql2, time,time,order_no);
		if(i>0) {
			return ServerResponse.serverResponseBySuccess();
		}
		else {
			return ServerResponse.serverResponseByError("error:发货失败，无受影响行数");
		}
		
		
	}
	//对状态的描述
	private String ToTypeDesc(long p) {
		if(p==1) {
			return "在线支付";
		}else {
			return "货到付款";
		}
	}
	private String ToStatusDesc(long s) {
		if(s==0) {
			return "已取消";
		}
		else if(s==1) {
			return "未付款";
		}
		else if(s==2) {
			return "已付款";
		}
		else if(s==3) {
			return "已发货";
		}
		else if(s==4) {
			return "交易成功";
		}
		else {
			return "交易关闭";
		}
	}
    
	//获取ordervo并返回,其中count>0时候肯定为list操作，=0为其他操作
	private ServerResponse BackOrdervo(List<orders> orderslist,int count){
		  //遍历
	    List<OrderVO> biglist=new ArrayList<OrderVO>();
	    for(orders o:orderslist) {
	    	OrderVO ordervo=new OrderVO();
	    	ordervo.setOrder_no(o.getOrder_no());
	    	ordervo.setPayment(o.getPayment());
	    	ordervo.setPayment_type(o.getPayment_type());
	    	ordervo.setPaymentTypeDesc(ToTypeDesc(o.getPayment_type()));
	    	ordervo.setPostage(o.getPostage());
	    	ordervo.setStatus(o.getStatus());
	    	ordervo.setStatusDesc(ToStatusDesc(o.getStatus()));
	    	ordervo.setPayment_time(o.getPayment_time());
	    	ordervo.setSend_time(o.getSend_time());
	    	ordervo.setEnd_time(o.getEnd_time());
	    	ordervo.setClose_time(o.getClose_time());
	    	ordervo.setCreate_time(o.getCreate_time());
	    	
	    	/////////
	    	//接下来添加detailList//////
	    	///////////
	    	//遍历当前order_no订单对应所有detail
	    	String detailsql="select * from order_detail where order_no=?";
	    	List<order_detail> dlist=jdbcUtil.select(order_detail.class, detailsql, o.getOrder_no());
	    	if(dlist.size()<=0) {
	    		return ServerResponse.serverResponseByError("error:该订单的详细情况不在数据库中");
	    	}
	    	List<OrderDetailVO> DVlist=new ArrayList<OrderDetailVO>();//存每个OrderDetailVO
	    	for(order_detail od:dlist) {
	    		OrderDetailVO odvo=new OrderDetailVO();
	    		odvo.setOrder_no(od.getOrder_no());
	    		odvo.setProduct_id(od.getProduct_id());
	    		odvo.setProduct_name(od.getProduct_name());
	    		odvo.setProduct_image(od.getProduct_image());
	    		odvo.setCurrent_unit_price(od.getCurrent_unit_price());
	    		odvo.setQuantity(od.getQuantity());
	    		odvo.setTotal_price(od.getTotal_price());
	    		odvo.setCreate_time(od.getCreate_time());
	    		DVlist.add(odvo);
	    	}
	    	//将orderItemVoList数组加入进去
	    	ordervo.setOrderDetailVOList(DVlist);
	    	ordervo.setImageHost("???");
	    	ordervo.setAddress_id(o.getAddress_id());
	    	
	    	////////////
	    	//addressvo操作区
	    	//////////
	    	String asql="select * from address where id=?";
	    	List<address> al=jdbcUtil.select(address.class,asql, o.getAddress_id());
	    	if(al.size()<=0) {
	    		return ServerResponse.serverResponseByError("error:此订单的详细地址不在数据库中");
	    	}
	    	address a=al.get(0);
	    	//装addressVO的箱
	    	AddressVO avo=new AddressVO();
	    	avo.setReceiver_name(a.getReceiver_name());
	    	avo.setReceiver_phone(a.getReceiver_phone());
	    	avo.setReceiver_mobile(a.getReceiver_mobile());
	    	avo.setReceiver_province(a.getReceiver_province());
	    	avo.setReceiver_city(a.getReceiver_city());
	    	avo.setReceiver_district(a.getReceiver_district());
	    	avo.setReceiverZip(a.getReceiver_zip());
	    	ordervo.setAddressVO(avo);
	    	
	    	//最后添加到最大数组中///
	    	biglist.add(ordervo);
	    }
	    //1-list  2-detail/search
	    
	    	return ServerResponse.serverResponseBySuccess(count, biglist, "ok");   	
	}
	
	
	
}
